/*
 * homostitch.cpp
 *
 *  Created on: Mar 23, 2013
 *      Author: rdu
 */

#include "homostitch.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/stitcher.hpp"

HomoStitch::HomoStitch()
{
	InitTransfMatrix();
}

HomoStitch::~HomoStitch()
{

}

void HomoStitch::InitTransfMatrix()
{
	Mat A1=(Mat_<double>(3,3)<<
			5.9488728159848210e+002, 0., 3.2229015408292554e+002,
			0.,5.9413758210164883e+002, 2.3704855673909827e+002,
			0., 0., 1.);

	Mat invA1=(Mat_<double>(3,3)<<
			0.0017, 0., -0.5418,
			0.,0.0017, -0.3990,
			0., 0., 1.);

	Mat R=(Mat_<double>(3,3)<<
			8.7432070927900074e-001, 2.4446238152750783e-002,4.8473258479912823e-001,
			-3.0139917703522961e-002,9.9953786573123260e-001, 3.9547857435484101e-003,
			-4.8441189362637443e-001, -1.8067551290407415e-002,8.7465346332343452e-001);

	Mat A2=(Mat_<double>(3,3)<<
			5.8720493128210046e+002, 0., 3.2943841218474392e+002,
			0.,5.8902398093762201e+002, 2.3992898076557407e+002,
			0., 0., 1.);

	Mat invA2=(Mat_<double>(3,3)<<
			0.0017, 0., -0.5610,
			0.,0.0017, -0.4073,
			0., 0., 1.);

//	Mat A1=(Mat_<double>(3,3)<<
//			3.8716761994393363e+002, 0., 3.2487612762472003e+002,
//			0.,4.0557488673193706e+002, 2.7861336272572305e+002,
//			0., 0., 1.);
//
//	Mat invA1=(Mat_<double>(3,3)<<
//			0.0026, 0., -0.8391,
//			0.,0.0025, -0.6870,
//			0., 0., 1.);
//
//	Mat R=(Mat_<double>(3,3)<<
//			7.9225979843054595e-001, -5.3657223000551799e-002,6.0782013310736904e-001,
//			1.1169922958459395e-001,9.9204698944995828e-001, -5.8017694141378362e-002,
//			-5.9987306482371661e-001, 1.1385812725995195e-001,7.9195241836606434e-001);
//
//	Mat A2=(Mat_<double>(3,3)<<
//			4.3424447434213482e+002, 0., 3.5006806236471914e+002,
//			0.,4.5305264516101147e+002, 3.1401244160593245e+002,
//			0., 0., 1.);
//
//	Mat invA2=(Mat_<double>(3,3)<<
//			0.0023, 0., -0.8062,
//			0.,0.0022, -0.6931,
//			0., 0., 1.);


	m_transfMatrix=A1*R*invA2;
}

void HomoStitch::StitchImages()
{
	Mat result;

	Mat image0=imread("Pair1_1_2.jpg");
	Mat image1=imread("Pair1_1_1.jpg");

	warpPerspective(image0,result,m_transfMatrix,Size(image0.cols+image1.cols,image0.rows));

//	Mat affine=(Mat_<double>(2,3)<<
//			1., 0., 0,
//			0., 1., -12.5618690825134738e+000);
//
//	warpAffine(result,result,affine,Size(result.cols,result.rows));

	Mat half(result,Rect(0,0,image0.cols,image0.rows));

	image1.copyTo(half);

	m_stitchedImage=result;
}

void HomoStitch::StitchImages(Mat src1, Mat src2)
{
	Mat result;

	warpPerspective(src1,result,m_transfMatrix,Size(src1.cols+src1.cols,src1.rows));

	//	Mat affine=(Mat_<double>(2,3)<<
	//			1., 0., 0,
	//			0., 1., -12.5618690825134738e+000);
	//
	//	warpAffine(result,result,affine,Size(result.cols,result.rows));

	Mat half(result,Rect(0,0,src1.cols,src1.rows));

	src2.copyTo(half);

	m_stitchedImage=result;
}

Mat HomoStitch::GetStitchedImage()
{
	return m_stitchedImage;
}
